package com.maigao.util;

import com.maigao.entity.Captcha;
import org.apache.axis.encoding.Base64;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Random;

/**
 * @program: maigao
 * @description:
 * @author: yongza
 * @create: 2021-09-24 15:33
 **/
public class CaptchaUtil {
    private static int WIDTH = 90;
    private static int HEIGHT = 20;
    private static int codeCount = 4;
    private static char[] codeSequence = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
            'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '2', '3', '4', '5', '6', '7', '8', '9'};

    public static Captcha create(int width, int height) {
        BufferedImage buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics gd = buffImg.getGraphics();
        Random random = new Random();
        gd.setColor(Color.WHITE);
        gd.fillRect(0, 0, width, height);
        Font font = new Font("Fixedsys", Font.BOLD, height - 5);
        // 设置字体。
        gd.setFont(font);

        // 画边框。
        gd.setColor(Color.BLACK);
        gd.drawRect(0, 0, width - 1, height - 1);

        // 随机产生40条干扰线，使图象中的认证码不易被其它程序探测到。
        gd.setColor(Color.BLACK);
        for (int i = 0; i < 30; i++) {
            int x = random.nextInt(width);
            int y = random.nextInt(height);
            int xl = random.nextInt(12);
            int yl = random.nextInt(12);
            gd.drawLine(x, y, x + xl, y + yl);
        }
        StringBuffer randomCode = new StringBuffer();
        int red = 0, green = 0, blue = 0;
        for (int i = 0; i < codeCount; i++) {
            String code = String.valueOf(codeSequence[random.nextInt(34)]);
            red = random.nextInt(255);
            green = random.nextInt(255);
            blue = random.nextInt(255);
            gd.setColor(new Color(red, green, blue));
            gd.drawString(code, (i + 1) * (width / codeCount - 5), height - 5);
            randomCode.append(code);
        }
        Captcha captcha = new Captcha();
        captcha.setCode(randomCode.toString());
        captcha.setImage(buffImg);
        return captcha;
    }

    /**
     * 生成一个map集合
     * code为生成的验证码
     * codePic为生成的验证码BufferedImage对象
     *
     * @return
     */
    public static Captcha create() {
        return create(WIDTH, HEIGHT);
    }


    //    public static String getBufferedImageToBase64(BufferedImage bufferedImage,String imageFormatName) throws IOException {
//        if(StringUtils.isBlank(imageFormatName)){
//            imageFormatName = "png";
//        }
//        ByteArrayOutputStream stream = new ByteArrayOutputStream();
//        ImageIO.write(bufferedImage, imageFormatName, stream);
//        String s = Base64.encode().encodeToString(stream.toByteArray());
//        return s;
//    }
    public static String getBase64(BufferedImage image) {
        ByteArrayOutputStream stream = null;
        try {
            //输出流
            stream = new ByteArrayOutputStream();
            ImageIO.write(image, "png", stream);
            return Base64.encode(stream.toByteArray());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (stream != null) {
                try {
                    stream.flush();
                    stream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }

        }
        return "";
    }

}
